class Solution(object):
    def addToArrayForm(self, num, k):
        """
        :type num: List[int]
        :type k: int
        :rtype: List[int]
        """
        k = [int(t) for t in list(str(k))]
        add = 0
        index1 = len(num) - 1
        index2 = len(k) - 1
        add = 0
        out = [0] * max(len(num), len(k))
        i = 0
        while index1 >= 0 and index2 >= 0:
            out[i] = (num[index1] + k[index2] + add) % 10
            add = (num[index1] + k[index2] + add) // 10
            index1 -= 1
            index2 -= 1
            i += 1
        while index1 >= 0:
            out[i] = (num[index1] + add) % 10
            add = (num[index1] + add) // 10
            index1 -= 1
            i += 1
        while index2 >= 0:
            out[i] = (k[index2] + add) % 10
            add = (k[index2] + add) // 10
            index2 -= 1
            i += 1
        out.reverse()
        if add >= 1:
            return [1] + out
        else:
            return out


